package com.lizardmind.haofudoctor.ui.studio.allPatients

import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import com.blankj.utilcode.util.SizeUtils
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.adapter.BaseAdapter
import com.linxiao.framework.adapter.DividerGridItemDecoration
import com.linxiao.framework.util.color
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ItemPatientBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.PatientBean
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.other.recyclerView
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * 工作室-患者 界面
 * Created by Extends on 2017/11/30 11:10
 */
class PatientsActivity: BaseActivity() {
    lateinit var rv : RecyclerView
    private val apt by lazy { BaseAdapter<PatientBean,ItemPatientBinding>(R.layout.item_patient, arrayListOf()) }
    override fun onCreatRootView(): View? {
        return linearLayout {
            orientation=LinearLayout.VERTICAL
            include<ConstraintLayout>(R.layout.include_title_text)
            linearLayout {
                linearStyle(this)
                imageView(R.mipmap.aboutme_about)
                textView("全部患者").lparams(0,-2,1f){leftMargin = SizeUtils.dp2px(10f)}
                textView("0人") {
                    setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.into_smart,0)
                    compoundDrawablePadding = SizeUtils.dp2px(10f)
                }
            }
            include<View>(R.layout.include_split_line)
            linearLayout {
                linearStyle(this)
                imageView(R.mipmap.aboutme_about)
                textView("近期未咨询").lparams(0,-2,1f){leftMargin = SizeUtils.dp2px(10f)}
                textView("0人") {
                    setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.into_smart,0)
                    compoundDrawablePadding = SizeUtils.dp2px(10f)
                }
            }
            textView("最新患者"){
                textSize = 12f
                leftPadding = SizeUtils.dp2px(10f)
            }.lparams(-1,SizeUtils.dp2px(30f))
            rv = recyclerView {
                id = R.id.test_recycler
                layoutManager = LinearLayoutManager(context)
                adapter = apt
                addItemDecoration(DividerGridItemDecoration(context, 1f, context.color(R.color.windowBackground)))
            }
        }
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        //初始化title
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "患者"

        observe {
            postApi<Request<List<PatientBean>>>(hashMapOf(
                    "action" to "patient",
                    "token" to DoctorInfo.token
            ), Consumer {
                it.dispose {
                    apt.clearAddAllData(it)
                }
            },time = 10*60)
        }
    }

    fun linearStyle(ll : _LinearLayout){
        ll.apply {
            lparams(-1,SizeUtils.dp2px(50f))
            gravity = Gravity.CENTER_VERTICAL
            orientation = LinearLayout.HORIZONTAL
            horizontalPadding = SizeUtils.dp2px(16f)
            backgroundColor = color(R.color.white)
        }
    }
}